package com.tour.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tour.common.ServerResponse;
import com.tour.dao.ScenicMapper;
import com.tour.pojo.EasyUITreeNode;
import com.tour.pojo.Scenic;
import com.tour.service.IScenicService;

/**
 * Created by tang
 */
@Service("iScenicService")
public class ScenicServiceImpl implements IScenicService {

    private Logger logger = LoggerFactory.getLogger(ScenicServiceImpl.class);

    @Autowired
    private ScenicMapper scenicMapper;

    public ServerResponse addScenic(String scenicName,Integer parentId){
        if(parentId == null || StringUtils.isBlank(scenicName)){
            return ServerResponse.createByErrorMessage("添加地址参数错误");
        }

        Scenic scenic = new Scenic();
        scenic.setScName(scenicName);
        scenic.setParentId(parentId);

        int rowCount = scenicMapper.insert(scenic);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("添加景点成功");
        }
        return ServerResponse.createByErrorMessage("添加景点失败");
    }

    public ServerResponse updateScenicName(Integer scenicId,String scenicName){
        if(scenicId == null || StringUtils.isBlank(scenicName)){
            return ServerResponse.createByErrorMessage("更新地址参数错误");
        }
        Scenic scenic = new Scenic();
        scenic.setScenicId(scenicId);
        scenic.setScName(scenicName);

        int rowCount = scenicMapper.updateByPrimaryKeySelective(scenic);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("更新景点名字成功");
        }
        return ServerResponse.createByErrorMessage("更新景点名字失败");
    }

    //根据父节点获取子节点(景点)
    public List<EasyUITreeNode> getChildrenParallelScenic(Integer scenicId){
        List<Scenic> scenicList = scenicMapper.selectScenicChildrenByParentId(scenicId);
        if(CollectionUtils.isEmpty(scenicList)){
            logger.info("未找到当前地址的子地址");
        }
        List<EasyUITreeNode> resultList = new ArrayList<EasyUITreeNode>();
		//把列表转换成EasyUITreeNode列表
		for (Scenic scenic : scenicList) {
			EasyUITreeNode node = new EasyUITreeNode();
			//设置属性
			node.setId(scenic.getScenicId());
			node.setText(scenic.getScName());
			node.setState(scenic.getIsParent()?"closed":"open");
			//添加到结果列表
			resultList.add(node);
		}
        return resultList;
    }
    
  //根据父节点获取子节点(地址)
    public List<EasyUITreeNode> getChildrenParallelAddress(Integer scenicId){
        List<Scenic> scenicList = scenicMapper.selectAddressChildrenByParentId(scenicId);
        if(CollectionUtils.isEmpty(scenicList)){
            logger.info("未找到当前地址的子地址");
        }
        List<EasyUITreeNode> resultList = new ArrayList<EasyUITreeNode>();
		//把列表转换成EasyUITreeNode列表
		for (Scenic scenic : scenicList) {
			EasyUITreeNode node = new EasyUITreeNode();
			//设置属性
			node.setId(scenic.getScenicId());
			node.setText(scenic.getScName());
			node.setState(scenic.getIsParent()&&!(scenic.getIsFinal())?"closed":"open");
			//添加到结果列表
			resultList.add(node);
		}
        return resultList;
    }


    /**
     * 递归查询本节点的id及孩子节点的id
     * @param categoryId
     * @return
     */
    public ServerResponse<List<Integer>> selectScenicAndChildrenById(Integer scenicId){
        Set<Scenic> scenicSet = Sets.newHashSet();
        findChildscenic(scenicSet,scenicId);


        List<Integer> categoryIdList = Lists.newArrayList();
        if(scenicId != null){
            for(Scenic scenicItem : scenicSet){
                categoryIdList.add(scenicItem.getScenicId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }


    //递归算法,算出子节点
    private Set<Scenic> findChildscenic(Set<Scenic> scenicSet ,Integer scenicId){
    	Scenic scenic = scenicMapper.selectByPrimaryKey(scenicId);
        if(scenic != null){
        	scenicSet.add(scenic);
        }
        //查找子节点,递归算法一定要有一个退出的条件
        List<Scenic> scenicList = scenicMapper.selectScenicChildrenByParentId(scenicId);
        for(Scenic scenicItem : scenicList){
            findChildscenic(scenicSet,scenicItem.getScenicId());
        }
        return scenicSet;
    }
}
